from Case.python_ui.Common.basepage import Basepage
from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException
import time, pytest
from Case.python_ui.PageLocate.user_my_voucher_locator import CouponMyVoucherLocator
from Case.python_ui.Pageobject.admin_coupon_voucher_template_page import VoucherTemplatePage
from Case.python_ui.PageLocate.admin_coupon_voucher_template_locator import AdminCouponVouchertemplateLocator
from Case.python_ui.PageLocate.admin_coupon_voucher_records_locator import AdminCouponVoucherRecordsLocator
from selenium.webdriver.common.by import By

class MyVoucherPage(Basepage):

    #判断是否包含元素
    def check_contain_str(self, string, except_string):
        if except_string in string:
            assert True
        else:
            assert False , '实际值:' + string + '期望值:' + except_string

    # 校验某个属性值是否符合预期
    def check_attribute(self, real_result, except_result):
        if real_result == except_result:
            assert True
        else:
            assert False, '实际值:' + real_result + '期望值:' + except_result

    # 元素是否存在
    def element_is_exist(self, locate):
        doc = '判断元素是否存在'
        try:
            self.get_element(locate, doc)
        except NoSuchElementException as e:
            return False
        else:
            return True

    # 清空筛选状态
    def clear_filter_status(self, locate):
        doc = '清空筛选状态'
        try:
            #点击筛选按钮
            self.click_element(locate, doc)
            time.sleep(2)
            #点击reset按钮
            self.click_element(CouponMyVoucherLocator.status_filter_reset_button, doc)
        except ElementClickInterceptedException as e:
            self.click_element(CouponMyVoucherLocator.status_filter_ok_button, doc)
            return
        else:
            self.click_element(CouponMyVoucherLocator.status_filter_ok_button, doc)
            return

    # 列表筛选功能
    def check_filter_result(self, search_num_locate, column_num, filter_button_locate, filter_arry):
        doc = '校验筛选结果'
        result_arry = []
        i = 1
        for selection in filter_arry:
            # 清空筛选条件
            self.clear_filter_status(filter_button_locate)
            time.sleep(2)
            filter_selection = self.assemble_filter_selection_locate(str(i))
            i = i + 1
            self.click_element(filter_button_locate, doc)
            time.sleep(2)
            # self.wait_elevisble(filter_selection, doc)
            self.click_element(filter_selection, doc)
            time.sleep(2)
            self.click_element(CouponMyVoucherLocator.status_filter_ok_button, doc)
            time.sleep(5)
            if self.element_is_exist(search_num_locate) is False:
                result_arry.append(False)
                continue
            search_result = self.get_element(search_num_locate, doc).text
            search_num = int(search_result.split(' ', 2)[1])
            count = 0
            if search_num <= 10:
                for j in range(1, search_num + 1):
                    real_result = self.get_element(self.assemble_list_element_locate(str(j), str(column_num)), doc).text
                    self.check_attribute(real_result, selection)
            if search_num > 10:
                for j in range(1, 11):
                    real_result = self.get_element(self.assemble_list_element_locate(str(j), str(column_num)), doc).text
                    self.check_attribute(real_result, selection)
            result_arry.append(True)
        # 最后清空筛选条件
        self.clear_filter_status(filter_button_locate)
        return result_arry

    def assemble_list_element_locate(self, row, column):
        list_locate = '(//tr[contains(@class,"antd4-table-row antd4-table-row-level-0")])[$_1]//td[$_2]'
        search_result = (By.XPATH, list_locate.replace('$_1', row).replace('$_2', column))
        return search_result

    def assemble_filter_selection_locate(self, num):
        list_locate = '(//input[contains(@class,"antd4-checkbox-input")])[$_1]'
        status_selection_locate = (By.XPATH, list_locate.replace('$_1', num))
        return status_selection_locate


    #进入My Voucher页面
    def enter_my_voucher_page(self):
        time.sleep(2)
        doc = "进入My Voucher页面"
        self.wait_elevisble(CouponMyVoucherLocator.my_voucher_menu, doc)
        self.click_element(CouponMyVoucherLocator.my_voucher_menu, doc)
        time.sleep(3)

    #校验列表元素
    #时区，货币单位需要单独提出来，根据配置文件来校验  --待优化
    def check_list_attribute(self):
        time.sleep(2)
        doc = "校验列表元素"
        my_voucher_list = ['Voucher No.','Status','Voucher Template','Fixed Amount','Balance','Reuse','Validation Period']
        my_voucher_list_no = self.get_element(CouponMyVoucherLocator.my_voucher_list_no, doc).text
        self.check_contain_str(my_voucher_list_no, my_voucher_list[0])
        my_voucher_list_status = self.get_element(CouponMyVoucherLocator.my_voucher_list_status, doc).text
        self.check_contain_str(my_voucher_list_status, my_voucher_list[1])
        my_voucher_list_template = self.get_element(CouponMyVoucherLocator.my_voucher_list_template, doc).text
        self.check_contain_str(my_voucher_list_template, my_voucher_list[2])
        my_voucher_list_fixed_amount = self.get_element(CouponMyVoucherLocator.my_voucher_list_fixed_amount, doc).text
        self.check_contain_str(my_voucher_list_fixed_amount, my_voucher_list[3])
        my_voucher_list_balance = self.get_element(CouponMyVoucherLocator.my_voucher_list_balance, doc).text
        self.check_contain_str(my_voucher_list_balance, my_voucher_list[4])
        my_voucher_list_reuse = self.get_element(CouponMyVoucherLocator.my_voucher_list_reuse, doc).text
        self.check_contain_str(my_voucher_list_reuse, my_voucher_list[5])
        my_voucher_list_period = self.get_element(CouponMyVoucherLocator.my_voucher_list_period, doc).text
        self.check_contain_str(my_voucher_list_period, my_voucher_list[6])
        time.sleep(3)

    #搜索voucher
    def search_voucher(self):
        time.sleep(2)
        doc = "搜索voucher"
        coupon_template = 'test自动下发'
        couppon_no = 'VON127TCCSDAAY2'
        search_input = [couppon_no, coupon_template]
        #清空搜索条件
        self.clear_filter_status(CouponMyVoucherLocator.status_filter_button)
        # 清空搜索框
        self.keyboard_event(CouponMyVoucherLocator.search_input, doc)
        # couppon_no查询
        self.send_keys(CouponMyVoucherLocator.search_input, search_input[0], doc)
        time.sleep(2)
        self.click_element(CouponMyVoucherLocator.search_button, doc)
        time.sleep(5)
        # 检查是否有查询结果
        self.check_attribute(self.get_element(CouponMyVoucherLocator.first_search_result, doc).text, search_input[0])
        # 清空搜索框
        self.keyboard_event(CouponMyVoucherLocator.search_input, doc)
        # coupon_template查询
        self.send_keys(CouponMyVoucherLocator.search_input, search_input[1], doc)
        time.sleep(2)
        self.click_element(CouponMyVoucherLocator.search_button, doc)
        time.sleep(5)
        # 检查是否有查询结果
        self.check_attribute(self.get_element(CouponMyVoucherLocator.first_search_result_voucher_template, doc).text, search_input[1])

    # 筛选 voucher
    def filter_voucher(self):
        time.sleep(2)
        status_arry = ['Valid', 'Exhausted', 'Expired']
        doc = '筛选 valid coupon'
        self.check_filter_result(CouponMyVoucherLocator.search_result_num, 2, CouponMyVoucherLocator.status_filter_button, status_arry)


    # bind voucher
    def bind_voucher(self, voucher_no):
        time.sleep(2)
        doc = 'bind voucher'
        self.wait_elevisble(CouponMyVoucherLocator.bind_voucher_button, doc)
        self.click_element(CouponMyVoucherLocator.bind_voucher_button, doc)
        #输入优惠券
        self.wait_elevisble(CouponMyVoucherLocator.bind_voucher_input, doc)
        self.send_keys(CouponMyVoucherLocator.bind_voucher_input, voucher_no, doc)
        time.sleep(2)
        # 点击ok按钮
        self.click_element(CouponMyVoucherLocator.ok_button, doc)
        time.sleep(2)
        #校验绑定结果
        #清空筛选条件
        self.clear_filter_status(CouponMyVoucherLocator.status_filter_button)
        time.sleep(2)
        self.wait_elevisble(CouponMyVoucherLocator.search_input, doc)
        self.send_keys(CouponMyVoucherLocator.search_input, voucher_no, doc)
        time.sleep(2)
        self.click_element(CouponMyVoucherLocator.search_button, doc)
        time.sleep(2)
        result = self.get_element(CouponMyVoucherLocator.first_search_result, doc).text
        self.check_attribute(result, voucher_no)


    #到admin端校验绑定结果
    def check_admin_bind_result(self, driver, voucher_no, voucher_template_name):
        time.sleep(2)
        VoucherTemplatePage(driver).enter_voucher_template_page()
        time.sleep(2)
        doc = '检查generate history列表的券编码是否是绑定状态'
        self.wait_elevisble(AdminCouponVouchertemplateLocator.search_input, doc)
        self.send_keys(AdminCouponVouchertemplateLocator.search_input, voucher_template_name, doc)
        time.sleep(2)
        self.click_element(AdminCouponVouchertemplateLocator.search_button, doc)
        time.sleep(5)
        assert self.get_element(AdminCouponVouchertemplateLocator.first_search_result, doc).text == voucher_template_name
        #点击详情
        self.click_element(AdminCouponVouchertemplateLocator.first_search_result, doc)
        time.sleep(2)
        self.wait_elevisble(AdminCouponVouchertemplateLocator.generate_history_tab, doc)
        self.click_element(AdminCouponVouchertemplateLocator.generate_history_tab, doc)
        time.sleep(2)
        #筛选已绑定的记录
        # 清空筛选条件
        self.clear_filter_status(AdminCouponVouchertemplateLocator.bind_filter_button)
        #筛选Y
        self.click_element(AdminCouponVouchertemplateLocator.bind_filter_button, doc)
        time.sleep(2)
        self.click_element(AdminCouponVouchertemplateLocator.bind_filter_y_button, doc)
        time.sleep(2)
        self.click_element(AdminCouponVouchertemplateLocator.bind_filter_ok_button, doc)
        #检查绑定状态是否是Y
        self.check_attribute(self.get_element(AdminCouponVouchertemplateLocator.first_history_voucher_template, doc).text, voucher_no)
        self.check_attribute(self.get_element(AdminCouponVouchertemplateLocator.first_history_voucher_template_result_bind, doc).text, 'Y')

        #检查voucher record列表的券编码是否是绑定状态
        doc = '检查voucher record列表的券编码是否是绑定状态'
        #进入voucher record 页面
        self.click_element(AdminCouponVoucherRecordsLocator.voucher_records_menu, doc)
        time.sleep(2)
        #voucher no 进行搜索
        self.wait_elevisble(AdminCouponVoucherRecordsLocator.search_input, doc)
        self.send_keys(AdminCouponVoucherRecordsLocator.search_input, voucher_no, doc)
        time.sleep(2)
        self.click_element(AdminCouponVoucherRecordsLocator.search_button, doc)
        time.sleep(5)
        #检查绑定状态是否是Y
        assert self.get_element(AdminCouponVoucherRecordsLocator.first_search_result, doc).text == voucher_no
        assert self.get_element(AdminCouponVoucherRecordsLocator.first_search_result_bind, doc).text == 'Y'















